﻿package view
{
    import com.core.*;
    import com.sh.game.consts.*;
    import com.sh.game.global.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;
    import manager.*;
    import view.loader.*;

    public class DialogMgr extends Object
    {
        private var mRegisterList:Dictionary;
        private var mDialogType:Dictionary;
        private var mDialogList:Dictionary;
        private var mWaitList:Array;
        private var mLoadComplete:Boolean = true;
        private var mLoader:UIAssetsLoader;
        private var mLoading:Boolean = false;
        private static var mInstance:DialogMgr;

        public function DialogMgr()
        {
            this.mWaitList = [];
            this.mRegisterList = new Dictionary();
            this.mDialogList = new Dictionary();
            this.mDialogType = new Dictionary();
            var _loc_1:* = Control.create(DialogControl, this);
            ModelProxy.addObserver("appdialog", _loc_1);
            return;
        }// end function

        private function loadSource() : void
        {
            this.mLoading = true;
            this.mLoader = new UIAssetsLoader();
            this.mLoader.addEventListener(Event.COMPLETE, this.loadComplete);
            this.mLoader.appendToList(Config.getUrl("assets/images/mornui/common.swf", ResourceType.NORMAL));
            this.mLoader.beginLoad();
            return;
        }// end function

        private function loadComplete(event:Event) : void
        {
            if (this.mLoader)
            {
                this.mLoader.removeEventListener(Event.COMPLETE, this.loadComplete);
                this.mLoader.dispose();
                this.mLoader = null;
            }
            this.mLoading = false;
            this.mLoadComplete = true;
            if (this.mWaitList.length > 0)
            {
                this.execute();
            }
            return;
        }// end function

        public function register(param1:uint, param2:Class, param3:int = 0) : void
        {
            this.mRegisterList[param1] = param2;
            this.mDialogType[param1] = param3;
            return;
        }// end function

        public function open(param1:uint, param2:Object = null) : void
        {
            if (AppDialog.COMPOUND_TIP == param1)
            {
                return;
            }
            if (this.mLoadComplete)
            {
                this.openDialog(param1, param2);
            }
            else
            {
                this.mWaitList.push([param1, param2]);
                if (!this.mLoading)
                {
                    this.loadSource();
                }
            }
            return;
        }// end function

        public function close(param1:uint) : void
        {
            this.closeDialog(param1);
            return;
        }// end function

        private function execute() : void
        {
            var _loc_1:Array = null;
            var _loc_2:int = 0;
            var _loc_3:Object = null;
            while (this.mWaitList.length > 0)
            {
                
                _loc_1 = this.mWaitList.shift();
                _loc_2 = _loc_1.shift();
                _loc_3 = _loc_1.shift();
                this.openDialog(_loc_2, _loc_3);
            }
            return;
        }// end function

        private function openDialog(param1:uint, param2:Object) : void
        {
            var _loc_3:* = this.getDialogUI(param1);
            var _loc_4:* = this.mDialogType[param1];
            if (_loc_3)
            {
                if (_loc_4 == 0)
                {
                    this.alertContainer.addChild(_loc_3);
                }
                else if (_loc_4 == 1)
                {
                    this.flashUiContainer.addChild(_loc_3);
                }
                _loc_3.show(param2);
            }
            return;
        }// end function

        private function closeDialog(param1:uint) : void
        {
            var _loc_2:IDialogUI = null;
            if (this.mDialogList[param1])
            {
                _loc_2 = this.getDialogUI(param1);
                if (_loc_2)
                {
                    _loc_2.close();
                    if ((_loc_2 as Object).parent)
                    {
                        (_loc_2 as Object).parent.removeChild(_loc_2);
                    }
                }
            }
            return;
        }// end function

        public function getDialogUI(param1:uint) : IDialogUI
        {
            if (!this.mDialogList[param1])
            {
                if (this.mRegisterList[param1])
                {
                    this.mDialogList[param1] = new this.mRegisterList[param1];
                }
            }
            return this.mDialogList[param1];
        }// end function

        public function dialogVisible(param1:uint) : Boolean
        {
            if (this.mDialogList[param1])
            {
                return Sprite(this.mDialogList[param1]).visible;
            }
            return false;
        }// end function

        private function get alertContainer() : Object
        {
            return PluginManager.instance.alertContainer;
        }// end function

        private function get flashUiContainer() : Object
        {
            return PluginManager.instance.flashUiContainer;
        }// end function

        public static function get instance() : DialogMgr
        {
            if (!mInstance)
            {
                mInstance = new DialogMgr;
            }
            return mInstance;
        }// end function

    }
}
